package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.common.core.constant.SupervisionBusinessConstants;
import com.engine4cloud.tdf.common.core.constant.enums.BriefAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.util.CodeUtils;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.supervision.dto.create.SpHelpCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.SpHelpResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.SpHelpUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.SpHelpEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Mapper
public interface SpHelpConvert {

    SpHelpConvert INSTANCE = Mappers.getMapper(SpHelpConvert.class);

    SpHelpEntity createTo(SpHelpCreateDTO s);

    SpHelpEntity updateTo(SpHelpUpdateDTO s);

    SpHelpResultDTO convertTo(SpHelpEntity s);

    default SpHelpEntity create(SpHelpCreateDTO s, Long tenantId, BriefAuditActionStatusEnum action) {
        SpHelpEntity entity = createTo(s);
        entity.setTenantId(tenantId);
        entity.setLcbh(CodeUtils.entrustCode());
        entity.setPreAction(action);
        additionalArea(entity, s.getAreaList());
        return entity;
    }

    default SpHelpEntity update(SpHelpUpdateDTO s) {
        SpHelpEntity entity = updateTo(s);
        additionalArea(entity, s.getAreaList());
        return entity;
    }

    default SpHelpResultDTO convert(SpHelpEntity s) {
        SpHelpResultDTO result = convertTo(s);
        renderAreaList(result);
        return result;
    }

    default void additionalArea(SpHelpEntity s, List<String> areaList) {
        if (Objects.isNull(s) || CollectionUtils.isEmpty(areaList)) {
            return;
        }
        s.setBfszs(areaList.get(SupervisionBusinessConstants.AREA_PROVINCE_INDEX));
        s.setBfszds(areaList.get(SupervisionBusinessConstants.AREA_CITY_INDEX));

        if (areaList.size() >= SupervisionBusinessConstants.AREA_COUNTY_ARRAY_LENGTH) {
            s.setBfszxq(areaList.get(SupervisionBusinessConstants.AREA_COUNTY_INDEX));
        } else {
            s.setBfszxq(null);
        }

        if (areaList.size() >= SupervisionBusinessConstants.AREA_VILLAGE_ARRAY_LENGTH) {
            s.setBfszd(areaList.get(SupervisionBusinessConstants.AREA_VILLAGE_INDEX));
        } else {
            s.setBfszd(null);
        }
    }

    default void renderAreaList(SpHelpResultDTO s) {
        if (Objects.isNull(s)) {
            return;
        }

        List<String> residenceAreaList = new ArrayList<>();
        Actions.apply(StringUtils.isNotEmpty(s.getBfszs()), () -> residenceAreaList.add(s.getBfszs()));
        Actions.apply(StringUtils.isNotEmpty(s.getBfszds()), () -> residenceAreaList.add(s.getBfszds()));
        Actions.apply(StringUtils.isNotEmpty(s.getBfszxq()), () -> residenceAreaList.add(s.getBfszxq()));
        Actions.apply(StringUtils.isNotEmpty(s.getBfszd()), () -> residenceAreaList.add(s.getBfszd()));

        s.setAreaList(residenceAreaList);
    }
}
